﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace InterrogazioneWebServicesBPM
{
    public class LoadBpmSecurityContextBusinessDelegate : AbstractBusinessDelegate
    {

        private LoadBpmSecurityContextWS.SecurityServicesGroupWSClient ssBpmClient;

        public LoadBpmSecurityContextBusinessDelegate(string sslUser, string userName, string realmDescription, string applicationName, string applicationCredential)
            : base(sslUser, userName, realmDescription, applicationName, applicationCredential, "") 
        {
            this.userNameValue = userName;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sslUser">input param for SSL Connection with WebSeal BPM</param>
        /// <param name="userName">input param for Usernametoken Authentication</param>
        /// <param name="realmDescription">input param for "BANCA del gruppo"</param>
        public LoadBpmSecurityContextBusinessDelegate(string sslUser, string userName, string realmDescription)
            : base(sslUser, userName, realmDescription, "SICUREZZA", "SICUREZZA", "") 
        {
            this.userNameValue = userName;
        }

        protected override void initInstance(string sslUser) {
            //inizializzazione del riferimento
            this.ssBpmClient = new LoadBpmSecurityContextWS.SecurityServicesGroupWSClient();
            //costruzione de contesto "certificato"
            //this.loginClient.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, sslUser);
        }

        /// <summary>
        /// Wrapper del servizio remoto di login
        /// </summary>
        /// <param name="uid">identificativo del cliente</param>
        /// <param name="pwd">password</param>
        /// <returns>da capire</returns>
        public LoadBpmSecurityContextWS.User getSecurityContext(string uid, string ambitoOperativo)
        {
            LoadBpmSecurityContextWS.User user = null;
            try
            {
                using (OperationContextScope scope = new OperationContextScope(this.ssBpmClient.InnerChannel))
                {
                    // SECURITY HEADER   
                   MessageHeader messageSecurityHeader = new SecurityHeader(this.userNameValue+"."+this.applicationName+"@"+this.realmDescription, this.passwordText);
                    Console.WriteLine("messageSecurityHeader:\n{0}", messageSecurityHeader.ToString());                
                    MessageHeaders mh = OperationContext.Current.OutgoingMessageHeaders;                    
                    mh.Add(messageSecurityHeader);
                    user = this.ssBpmClient.getSecurityContext(uid, ambitoOperativo, null, null);
                }
            }
            catch (TimeoutException timeProblem)
            {
                Console.WriteLine("The service operation timed out. {0} ", timeProblem.Message);
                this.ssBpmClient.Abort();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. {0}", commProblem.Message);
                this.ssBpmClient.Abort();

            }
            finally
            {
                if (this.ssBpmClient != null) this.ssBpmClient.Close();
            }
            return user;
            
        }
    }
}
